package com.finance.logistics.anlyzier;

import com.alibaba.fastjson.JSONObject;
import com.finance.framework.lang.Privilege;
import com.finance.system.bean.pojo.LogisticsColumnDef;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 字段值匹配映射类
 * Created by CBWL on 2017/4/27.
 */
public class HandlerHelper {
    private static Logger logger = LoggerFactory.getLogger(HandlerHelper.class);

    public static int analyze(Integer direction,LogisticsColumnDef logisticsColumnDef){
        if(direction == null) {
            return -1;
        }
        String mapping = logisticsColumnDef.getMapping();
        if(StringUtils.isBlank(mapping)||"null".equalsIgnoreCase(mapping)){
            return -1;
        }
        try {
            Map<String,Object> map = JSONObject.parseObject(mapping);
            for(Map.Entry<String, Object> entry : map.entrySet()){
                if(entry.getKey().equals(direction + "")){
                     return Integer.parseInt(entry.getValue().toString());
                }
            }
            return -1;
        } catch (Exception e) {
            logger.error("对象转换时错误{}",e.getMessage());
        }
        return -1;
    }

    public static String parse(String priveleges, Privilege privilege){
        Long prvs = Long.valueOf(priveleges);
        Long newPriv = Privilege.grant(prvs,privilege);
        return newPriv + "";
    }

    public static String parse(String priveleges, List<Privilege> privilegeList){
        Long prvs = Long.valueOf(priveleges);
        if(CollectionUtils.isEmpty(privilegeList)) {
            return priveleges;
        }
        Long newPriv = Privilege.grant(prvs, privilegeList.remove(0));
        for(Privilege pv : privilegeList){
            newPriv = Privilege.grant(newPriv, pv);
        }
        return newPriv + "";
    }

}
